package pl.edu.agh.anomalyids.lerad.aspect.implementation;

import pl.edu.agh.anomalyids.lerad.aspect.AbstractAspect;
import pl.edu.agh.anomalyids.lerad.aspect.enums.AspectType;
import pl.edu.agh.anomalyids.lerad.aspect.enums.Match;
import pl.edu.agh.anomalyids.network.Packet;
import pl.edu.agh.anomalyids.network.Packet.Protocol;
import pl.edu.agh.anomalyids.network.header.Header;
import pl.edu.agh.anomalyids.network.header.impl.IPv4Header;

public class TtlAspect extends AbstractAspect<Integer> {
	
	private static final long serialVersionUID = 1L;

	/**
	 * Konstruktor. Nalezy podac port, jaki chcemy akceptowac poprzez
	 * validate().
	 * 
	 * @param ttl
	 *            wartosc portu
	 * @param type
	 *            typ cechy
	 */
	public TtlAspect(Integer ttl) {
		super(AspectType.TTL);
		this.values.add(ttl);
	}

	@Override
	public String toString() {
		return getData().toString();
	}

	@Override
	public Match packetMatchesAspect(Packet packet) {
		Header header = packet.getProtocols().get(Protocol.IPv4);
		if (header != null) {
			IPv4Header ip = (IPv4Header) header;
			if (validate(ip.getTTL())) {
				return Match.SAME;
			} else {
				return Match.DIFFER;
			}
		} else {
			return Match.UNKNOWN;
		}
	}
}
